
import java.util.Stack;

/**
 * Author:kongguoguang
 * Date:2020/7/8
 * Time:15:18
 * Summary:
 */
public class QueueByStack<E>{


    public static void main(String[] args) {
        QueueByStack<String> queue = new QueueByStack<>();
        queue.enqueue("a");
        queue.enqueue("b");
        System.out.println(queue.dequeue());
        queue.enqueue("c");
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());
    }


    private Stack<E> stackOne = new Stack<>();

    private Stack<E> stackTwo = new Stack<>();


    /**
     * 入队
     */
    public void enqueue(E e) {
        stackOne.push(e);
    }


    /**
     * 出队
     */
    public E dequeue() {
        if (stackTwo.isEmpty()){
            while (!stackOne.isEmpty()){
                stackTwo.push(stackOne.pop());
            }
        }

        if (stackTwo.isEmpty()){
            return null;
        } else {
            return stackTwo.pop();
        }
    }


    /**
     * 检索队头元素
     */
    public E peek() {
        if (stackTwo.isEmpty()){
            while (!stackOne.isEmpty()){
                stackTwo.push(stackOne.pop());
            }
        }

        if (stackTwo.isEmpty()){
            return null;
        } else {
            return stackTwo.peek();
        }
    }

    public boolean isEmpty(){
        return stackOne.isEmpty() && stackTwo.isEmpty();
    }

}
